package com.hzdl.car.contorlle;

import com.github.pagehelper.PageInfo;
import com.hzdl.car.bean.*;
import com.hzdl.car.service.CheckService;
import com.hzdl.car.service.RentService;
import com.hzdl.car.util.HzdlResultJson;
import com.hzdl.car.vo.CheckVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("check")
public class CheckControlle {
    @Autowired
    private CheckService service;
    @Autowired
    private RentService rentService;

    @RequestMapping("init")
    public HzdlResultJson init(String rentid, HttpSession session){
        Rent checkVo1= service.findById(rentid);
        Car car = rentService.findByrentId(rentid);
        Customer customer = service.findByidentity(rentid);
        session.setAttribute("customer",customer);
        Map<String,Object> map=new HashMap<>();
        map.put("checkVo1",checkVo1);
        map.put("car",car);
        map.put("customer",customer);
        if (checkVo1==null){
            return new  HzdlResultJson(-1,"没有该记录");
        }
        return new HzdlResultJson(map);
    }

    @RequestMapping("add")
    public HzdlResultJson add(String rentid,CheckVo checkVo){
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String checkdate1 = checkVo.getCheckdate1();
            if (checkdate1!=null&& !checkdate1.isEmpty()){
                try {
                    Date parse = simpleDateFormat.parse(checkdate1);
                    checkVo.setCheckdate(parse);
                    int row = service.add(checkVo);
                    if (row>0){
                        Car car1 = service.findRentandCar(rentid);
                        service.rentUpdate(rentid);
                        service.carnumberUpdate1(car1.getCarnumber());
                        return  HzdlResultJson.ADD_SUCCESS();
                    }

                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }

        return   HzdlResultJson.ADD_FAIL();
    }


    @RequestMapping("checkAll")
    public HzdlResultJson checkAll(CheckVo checkVo){
        PageInfo<Check> pageInfo = service.findWhere(checkVo);
        return new HzdlResultJson(pageInfo.getTotal(),pageInfo.getList());
    }

    @RequestMapping("update")
    public HzdlResultJson update(CheckVo checkVo){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String checkdate1 = checkVo.getCheckdate1();
        if (checkdate1!=null&& !checkdate1.isEmpty()){
            try {
                Date parse = simpleDateFormat.parse(checkdate1);
                checkVo.setCheckdate(parse);
                int row = service.update(checkVo);
                if (row>0){
                    return new HzdlResultJson("修改成功",null);
                }

            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        return new HzdlResultJson(-1,"修改失败");

    }

    @RequestMapping("delete")
    public HzdlResultJson delete (String checkid){
        int row = service.delete(checkid);
        if (row>0){
            return HzdlResultJson.DELETE_SUCCESS();
        }else{
            return HzdlResultJson.DELETE_FAIL();
        }
    }

    @RequestMapping("deleteAll")
    public HzdlResultJson deleteAll ( @RequestParam(value = "ids") String ids){
        int row =0;
        String[] split = ids.split(",");

        for(String  id:split){
            row+=service.delete(id);
        }

        if (row>0){
            return HzdlResultJson.DELETE_ALL_SUCCESS();
        }else{
            return HzdlResultJson.DELETE_ALL_FAIL();
        }
    }
}
